//http.ts
import router from "@/router";
import axios from "axios";
import NProgress from "nprogress";
import { get, remove } from "./auth";

axios.defaults.baseURL = import.meta.env.DEV ? '/api' : ''
axios.defaults.timeout = 1000 * 60 * 5;
axios.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
axios.defaults.headers.put["Content-Type"] = "application/json;charset=UTF-8";
axios.defaults.headers.delete["Content-Type"] =
    "application/json;charset=UTF-8";
axios.interceptors.request.use(
    (config: any) => {
        const token = get();
        if (token) {
            config.headers["Authorization"] = "Bearer " + token;
        }
        return config;
    },
    (error) => {
        return error;
    }
);
// 响应拦截
axios.interceptors.response.use(
    (res) => {
        if (res.data.code == 401) {
            remove();
            router.push("/login");
            return res;
        } else {
            return res;
        }
    },
    (err) => {
        console.log(err);

        return err;
    }
);

export interface ResType<T> {
    code: number;
    data: T;
    msg: string;
}
export interface Http {
    get<T>(url: string, params?: unknown, headers?: unknown): Promise<ResType<T>>;
    post<T>(url: string, params?: unknown): Promise<ResType<T>>;
    put<T>(url: string, params?: unknown): Promise<ResType<T>>;
    delete<T>(url: string, params?: unknown): Promise<ResType<T>>;
    upload<T>(url: string, params: unknown): Promise<ResType<T>>;
    downloadFile(url: string, params: unknown): Promise<any>;
    gDownFile(url: string, params?: unknown): Promise<any>;
    download(url: string): void;
    redirect(url: string): Promise<any>;
}

const http: Http = {
    get(url, params) {
        return new Promise((resolve, reject) => {
            NProgress.start();
            axios
                .get(url, { params })
                .then((res) => {
                    NProgress.done();
                    resolve(res.data);
                })
                .catch((err) => {
                    NProgress.done();
                    reject(err.data);
                });
        });
    },
    redirect(url) {
        return new Promise((resolve, reject) => {
            NProgress.start();
            axios
                .get(url, {
                    headers: {
                        "Content-Type": "application/x-www-form-urlencoded",
                    },
                })
                .then((res) => {
                    NProgress.done();
                    resolve(res.data);
                })
                .catch((err) => {
                    NProgress.done();
                    reject(err.data);
                });
        });
    },
    post(url, params) {
        return new Promise((resolve, reject) => {
            NProgress.start();
            axios
                .post(url, JSON.stringify(params))
                .then((res) => {
                    NProgress.done();
                    resolve(res.data);
                })
                .catch((err) => {
                    NProgress.done();
                    reject(err.data);
                });
        });
    },
    put(url, params) {
        return new Promise((resolve, reject) => {
            NProgress.start();
            axios
                .put(url, JSON.stringify(params))
                .then((res) => {
                    NProgress.done();
                    resolve(res.data);
                })
                .catch((err) => {
                    NProgress.done();
                    reject(err.data);
                });
        });
    },
    delete(url, params) {
        return new Promise((resolve, reject) => {
            NProgress.start();
            axios
                .delete(url, { data: params })
                .then((res) => {
                    NProgress.done();
                    resolve(res.data);
                })
                .catch((err) => {
                    NProgress.done();
                    reject(err.data);
                });
        });
    },
    upload(url, file) {
        return new Promise((resolve, reject) => {
            NProgress.start();
            axios
                .post(url, file, {
                    headers: { "Content-Type": "multipart/form-data" },
                })
                .then((res) => {
                    NProgress.done();
                    resolve(res.data);
                })
                .catch((err) => {
                    NProgress.done();
                    reject(err.data);
                });
        });
    },
    downloadFile(url, file) {
        return new Promise((resolve, reject) => {
            NProgress.start();
            axios
                .post(url, file, {
                    responseType: "blob",
                })
                .then((res) => {
                    NProgress.done();
                    resolve(res);
                })
                .catch((err) => {
                    NProgress.done();
                    reject(err.data);
                });
        });
    },
    gDownFile(url, file) {
        return new Promise((resolve, reject) => {
            NProgress.start();
            axios
                .get(url, { params: file, responseType: "blob" })
                .then((res) => {
                    NProgress.done();
                    resolve(res);
                })
                .catch((err) => {
                    NProgress.done();
                    reject(err.data);
                });
        });
    },
    download(url) {
        const iframe = document.createElement("iframe");
        iframe.style.display = "none";
        iframe.src = url;
        iframe.onload = function () {
            document.body.removeChild(iframe);
        };
        document.body.appendChild(iframe);
    },
};
export default http;
